<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<body>
<!--表头导航-->
<script type="text/html" id="barDemo">
    <a shiro:hasPermission="character:question:add" class="layui-btn layui-btn-xs" lay-event="save"><i class="layui-icon layui-icon-add-circle"></i>添加</a>
    <a shiro:hasPermission="character:question:delete" class="layui-btn layui-btn-xs layui-btn-danger " lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<!--右侧导航-->
<script type="text/html" id="rowBarDemo">
    <a shiro:hasPermission="character:question:edit" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>
    <a shiro:hasPermission="character:question:delete" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<!--添加-->
<form class="layui-form" action="" id="saveQuestionForm" style="display: none;margin-right: 40px;margin-top: 20px"  >
    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="question" required  lay-verify="required" placeholder="请输入问题描述" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="optionA" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="optionB" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="optionC" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="optionD" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required">
                <option value="1">前15题</option>
                <option value="2">后15题</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" value="0" checked lay-skin="switch" lay-text="开启|关闭">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-block">
            <input type="text" name="creator" required  lay-verify="required" placeholder="请输入问题描述" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveQuestionFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--修改-->
<form class="layui-form" action=""  lay-filter="example" id="updateQuestionForm" style="display: none;margin-right: 40px;margin-top: 20px"  >
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="question" required  lay-verify="required" placeholder="请输入问题描述" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="optionA" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="optionB" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="optionC" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="optionD" placeholder="请输入内容" required  lay-verify="required"  class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="type" lay-verify="required">
                <option value="1">前15题</option>
                <option value="2">后15题</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" value="0" checked lay-skin="switch" lay-text="开启|关闭">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">修改人</label>
        <div class="layui-input-block">
            <input type="text" name="updater" required  lay-verify="required" placeholder="请输入问题描述" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateQuestionFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--模糊查询-->
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" name="question" placeholder="请输入问题" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="creator" placeholder="请输入创建人" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="selectFormfilter">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary"  id="myButton" >重置</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="test"></table>
</body>
<script>
    layui.use(['table','jquery','form'],function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;

        //第一个实例
        var myTable=table.render({
            elem: '#demo'
            ,height: 500
            ,url: '/question/selectAllPage' //数据接口
            ,page: true //开启分页
            ,limit:10
            ,limits:[5,10,15,20,25,30,50,100]
            ,toolbar: '#barDemo'
            ,cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                    ,{field: 'question', title: '问题', width:120,sort: true}
                    ,{field: 'optionA', title: '选项A', width:120}
                    ,{field: 'optionB', title: '选项B', width:120}
                    ,{field: 'optionC', title: '选项C', width:120}
                    ,{field: 'optionD', title: '选项D', width:120}
                    ,{field: 'type', title: '类型', width:120,
                    templet: function(d){
                        //得到当前行数据，并拼接成自定义模板
                        if(d.type==1){
                            //return '可用'
                            return 'FPA前15题'
                        }else if(d.type==2)
                        {
                            return 'FPA后15题'
                        }else{
                            return '未知类型'
                        }

                    }}
                    ,{field: 'status', title: '状态', width:80,
                    templet: function(d){
                        //得到当前行数据，并拼接成自定义模板
                        if(d.status==0){
                            //return '可用'
                            return '<a class="layui-btn  layui-btn-xs">可用</a>'
                        }else
                        {
                            return '<a class="layui-btn layui-btn-disabled  layui-btn-xs">失效</a>'
                        }

                    }
                }
                    ,{field: 'createTime', title: '创建时间', width:170}
                    ,{field: 'creator', title: '创建人', width:100}
                    ,{field: 'updateTime', title: '修改时间', width:170}
                    ,{field: 'updater', title: '修改人', width:100}
                    ,{fixed: 'right', width: 120, align:'center', toolbar: '#rowBarDemo'}

                ]
            ]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'save':
                    layer.open({
                        title: '添加问题',
                        type: 1,
                        area: ['500px', '450px'],
                        content: $("#saveQuestionForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        layer.confirm('真的删除选中的行么', function(index){
                            //obj.del(); //删除对应行（tr）的DOM结构
                            //删除数据库中的数据
                            console.log(data)
                            var idList=[];
                            //遍历数组data，抽取所有的id
                            for (let i = 0; i < data.length; i++) {
                                idList[i]=(data[i].id);
                            }
                            $.ajax({
                                url:"/question/delete",
                                type: "POST",
                                dataType:"JSON",
                                data:{idList:idList},
                                success:function (ret) {
                                    if(ret.code==0){
                                        //刷新表格数据
                                        myTable.reload({
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                        layer.msg(ret.msg,{icon: 6});
                                    }else
                                    {
                                        layer.msg(ret.msg,{icon: 5});
                                    }
                                }
                            })
                            layer.close(index);
                            //向服务端发送删除指令
                        });
                    }
                    break;
            };
        });

        //添加表单
        form.on('submit(saveQuestionFilter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //提交表单数据
            $.ajax({
                url:"/question/insert",
                type: "POST",
                dataType:"JSON",
                contentType:"application/json;charset=UTF-8",
                data:JSON.stringify(data.field),
                success:function (ret) {
                    if(ret.code==0){
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        myTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.data.question+ret.msg,{icon: 6});
                    }else
                    {
                        layer.msg(ret.msg,{icon: 5});
                    }

                }
            })
            return false;
            //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'delete'){
                layer.confirm('真的删除行么', function(index){
                    //obj.del(); //删除对应行（tr）的DOM结构
                    //删除数据库中的数据
                    console.log(data)
                    var idList=[];
                    //遍历数组data，抽取所有的id
                    idList.push(data.id);
                    $.ajax({
                        url:"/question/delete",
                        type: "delete",
                        dataType:"JSON",
                        data:{idList:idList},
                        success:function (ret) {
                            if(ret.code==0){
                                //刷新表格数据
                                myTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                                layer.msg(ret.msg,{icon: 6});
                            }else
                            {
                                layer.msg(ret.msg,{icon: 5});
                            }
                        }
                    })
                    layer.close(index);
                    //向服务端发送删除指令
                });
            }else if(layEvent === 'update'){
                console.log("=====================")
                console.log(data)
                //表单弹出之前进行数据的填充，也叫做表单赋值
                //表单赋值
                form.val('example', {
                    "question": data.question, // "name": "value"
                    "optionA": data.optionA, // "name": "value"
                    "optionB":data.optionB, // "name": "value"
                    "optionC":data.optionC, // "name": "value"
                    "optionD": data.optionD, // "name": "value"
                    "type": data.type, // "name": "value"
                    "status":data.status==0?true:false,
                    "updater":"",
                    "id":data.id
                });
                layer.open({
                    title: '修改问题',
                    type: 1,
                    area: ['500px', '450px'],
                    content: $("#updateQuestionForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });
        form.on('submit(updateQuestionFilter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //提交表单数据
            $.ajax({
                url:"/question/update",
                type: "PUT",
                dataType:"JSON",
                contentType:"application/json;charset=UTF-8",
                data:JSON.stringify(data.field),
                success:function (ret) {
                    if(ret.code==0){
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        myTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg,{icon: 6});
                    }else
                    {
                        layer.msg(ret.msg,{icon: 5});
                    }

                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //====================================提交条件查询==================================================
        form.on('submit(selectFormfilter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    question: data.field.question
                    ,creator: data.field.creator
                }
                ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            //提交表单数据
            /* $.ajax({
                 url:"/question/selectAllPage",
                 type: "POST",
                 dataType:"JSON",
                 data:data.field,
                 success:function (ret) {
                     if(ret.code==0){
                         //刷新表格数据
                         myTable.reload({
                             where: { //设定异步数据接口的额外参数，任意设
                                 question: data.field.question
                                 ,creator: data.field.creator
                             }
                             ,
                             page: {
                                 curr: 1 //重新从第 1 页开始
                             }
                         });
                     }else
                     {
                         layer.msg(ret.msg,{icon: 5});
                     }

                 }
             })*/
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        ///重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    question: ""
                    ,creator: ""
                    //…
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })

    });
</script>
</html>
